package com.azkj.album.view.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("AppCompatCustomView")
public class MyEditText extends EditText {
    private boolean isAddWatcher = false;
    private List<TextWatcher> watchers;

    public MyEditText(Context context) {
        super(context);
    }

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void addTextChangedListener(TextWatcher watcher) {
        super.addTextChangedListener(watcher);
        if (watchers == null) watchers = new ArrayList<>();
        watchers.add(watcher);
        isAddWatcher = true;
    }

    public void removeAllWatcher() {
        if (watchers != null && watchers.size() > 0) {
            for (TextWatcher watcher : watchers) {
                removeTextChangedListener(watcher);
            }
            watchers.clear();
        }
        isAddWatcher = false;
    }

    public boolean isAddWatcher() {
        return isAddWatcher;
    }
}
